<form id="addOrUpdateMerForm" lay-filter="addOrUpdateForm" class="layui-form model-form">

    <input name="id" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label layui-form-required">立功标题</label>
        <div class="layui-input-block">
            <input name="merTitle" class="layui-input" placeholder="请输入立功经历标题" lay-verType="tips" lay-verify="required" required/>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label ">立功内容</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入立功经历内容" class="layui-textarea" name="merContent" lay-verType="tips" id="merEditor" ></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label layui-form-required">立功时间</label>
        <div class="layui-input-block">
            <input type="text" name="merTime" id="merTimeId" placeholder="请选择立功经历时间" autocomplete="off" class="layui-input">
        </div>
    </div>

<!--    <div class="layui-form-item">-->
<!--        <label class="layui-form-label layui-form-required">工作时长</label>-->
<!--        <div class="layui-input-block">-->
<!--            <div class="layui-col-md11">-->
<!--                <input name="jobDuration" type="number" class="layui-input" placeholder="请输入工作经历时长" lay-verType="tips" lay-verify="required" required/>-->
<!--            </div>-->
<!--            <div class="layui-col-md1">-->
<!--                <label class="layui-form-label" style="width: auto;">年</label>-->
<!--            </div>-->
<!--        </div>-->

<!--    </div>-->

    <div class="layui-form-item">
        <label class="layui-form-label ">备注</label>
        <div class="layui-input-block">
            <input name="remark" class="layui-input" placeholder="请输入备注" lay-verType="tips"/>
        </div>
    </div>

    <div class="layui-form-item text-right">
        <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        <button class="layui-btn" lay-filter="formSubmitBtn1" lay-submit>保存</button>
    </div>
</form>

<!-- js部分 -->
<script src="${ctxPath}/assets/libs/tinymce/tinymce.min.js"></script>
<script>
    layui.use(['layer', 'form', 'admin', 'xnUtil', 'xmSelect', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var xnUtil = layui.xnUtil;
        var xmSelect = layui.xmSelect;
        var laydate = layui.laydate;

        laydate.render({
            elem: '#merTimeId'
        });

        var editData = admin.getLayerData('#addOrUpdateMerForm').data;
        var formUserId = admin.getTempData('formUserId');

        // 渲染富文本编辑器
        tinymce.init({
            selector: '#merEditor',
            height: 325,
            branding: false,
            language: 'zh_CN',
            plugins: 'code print preview fullscreen paste searchreplace save autosave link autolink image imagetools media table codesample lists advlist hr charmap emoticons anchor directionality pagebreak quickbars nonbreaking visualblocks visualchars wordcount',
            toolbar: 'fullscreen preview code | undo redo | forecolor backcolor | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | formatselect fontselect fontsizeselect | link image media emoticons charmap anchor pagebreak codesample | ltr rtl',
            toolbar_drawer: 'sliding',
            file_picker_types: 'media',
            relative_urls: false,
            remove_script_host: false,
            document_base_url: getProjectUrl(),
            images_upload_handler: function (blobInfo, success, failure, progress) {
                var xhr, formData;
                var file = blobInfo.blob();//转化为易于理解的file对象
                xhr = new XMLHttpRequest();
                xhr.withCredentials = false;
                xhr.open('POST', getProjectUrl() + 'sysFileInfo/upload');
                xhr.upload.onprogress = function (e) {
                    progress(e.loaded / e.total * 100);
                }
                xhr.onload = function () {
                    var res = JSON.parse(xhr.responseText);
                    if (!res.success) {
                        layui.notice.msg(res.message, {icon: 2});
                        return false;
                    } else {
                        success(getProjectUrl() + 'sysFileInfo/preview?id=' + res.data);
                    }
                };
                formData = new FormData();
                formData.append('file', file, file.name);//此处与源文档不一样
                xhr.send(formData);
            }, init_instance_callback: function (editor) {
                admin.removeLoading();
                if (editData !== null && editData !== undefined) {
                    admin.req(getProjectUrl() + 'sysUserMer/detail?id=' + editData.id, function (res) {
                        form.val('addOrUpdateForm', res.data);
                        editor.setContent(res.data.merContent);
                    });
                }
            }
        });

        /**
         * 表单提交事件
         */
        form.on('submit(formSubmitBtn1)', function (data) {
            var content = tinymce.get('merEditor').getContent();
           /* if (content === null || content === undefined || content === '') {
                layui.notice.msg('立功内容不能为空', {icon: 2});
                return false;
            }*/
            layer.load(2);
            var url = editData ? getProjectUrl() + 'sysUserMer/edit' : getProjectUrl() + 'sysUserMer/add';
            data.field.merContent = content;
            if(formUserId !== null && formUserId !== undefined){
                data.field.userId = formUserId;
            }

            admin.req(url, JSON.stringify(data.field), function(res){
                layer.msg(res.message, {icon: 1, time: 1000}, function () {
                    admin.putLayerData('formOk', true, '#addOrUpdateMerForm');
                    admin.closeDialog('#addOrUpdateMerForm');
                });
            }, 'post');
            return false;
        });
    });
</script>
</body>
</html>
